package yxy.game.pm2.active.module.sdk.shengye;

import org.joda.time.DateTime;
import org.redisson.api.RMap;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.module.BaseGameRedisLockBean;
import yxy.game.pm2.module.conquest.ConquestHandle;
import yxy.game.pm2.module.conquest.ConquestModule;

import java.util.HashMap;
import java.util.Map;

public class ShengYeSubscribeModule extends BaseGameRedisLockBean {

	//bean --------------------------------------------------------
	private String playercode;
	private long time;
//	private ArrayList<String> template = new ArrayList<String>();	//模板id
//	private int type;	//当前订阅活动分类：1离线收益提醒；2活动提醒
	
	public String getPlayercode() {
		return playercode;
	}
	public void setPlayercode(String playercode) {
		this.playercode = playercode;
	}
	public long getTime() {
		return time;
	}
	public void setTime(long time) {
		this.time = time;
	}
	
//	public ArrayList<String> getTemplate() {
//		return template;
//	}
//	public void setTemplate(ArrayList<String> template) {
//		this.template = template;
//	}
//	public int getType() {
//		return type;
//	}
//	public void setType(int type) {
//		this.type = type;
//	}
	
	// func -------------------------------------------------------------------------
	
	public static long nexttimes(String playercode) {
		ConquestModule conquestmodule = ConquestModule.get(playercode);
		long maxtimes = new ConquestHandle().get挂机最大时长(playercode);
		DateTime startTime = conquestmodule.timeline().getStartTime();
		
		long nexttimes = startTime.getMillis() + maxtimes;
		return nexttimes;
	}
	
	public static void updatenexttimes(String playercode) {
		if(ShengYeSubscribeModule.all().containsKey(playercode)) {
			long nexttimes = nexttimes(playercode);
			try {
				ShengYeSubscribeModule.touch().put(playercode, nexttimes);
			} catch (RedisException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
	}
	
	// redis==========================================================================

//	static public long get(String playercode) {
//		try {
//			long value = touch().get(playercode);
//			return value;
//		} catch (Exception e) {
//			return 0;
//		}
//	}

	static public RMap<String, Long> touch() throws RedisException {
		return RedisManager.client(redis()).getMap(key());
	}

	static public Map<String, Long> all() {
		try {
			Map<String, Long> sdkparms = touch().readAllMap();
			return sdkparms;
		} catch (RedisException e) {
		}
		return new HashMap<>();
	}
	
	static public String key() {
		return "SDK:SHENGYESUBSCRIBE:1";
	}

	static private String lockKey() {
		return String.format("lock:%s", key());
	}
	
	static public boolean lockShengYeSubscribe() throws InterruptedException {
		return tryLock(lockKey());
	}

	static public void unlockShengYeSubscribe() {
		unlock(lockKey());
	}
}
